package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.GuidePageAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.utils.AppManagerDelegate;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;


public class GlideActivity extends BaseActivity implements ViewPager.OnPageChangeListener,View.OnClickListener {
    private ViewPager vp;
    private int []imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合
    private RelativeLayout guide_ib_start; //立即体验
    private ViewGroup vg;//放置圆点;
    //实例化原点View
    private ImageView iv_point;
    private ImageView []ivPointArray;
    @Override
    public int intiLayout() {
        return R.layout.activity_glide;
    }

    @Override
    public void initView() {
        ImmersionBar mImmersionBar = ImmersionBar.with(this);
        mImmersionBar
                .statusBarColor(R.color.colorWhite)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true).init();   //所有子类都将继承这些相同的属性
        vp=findViewById(R.id.guide_vp);
        guide_ib_start=findViewById(R.id.guide_ib_start);
        guide_ib_start.setOnClickListener(this);
        vp = (ViewPager) findViewById(R.id.guide_vp);
    }

    @Override
    public void initData() {
        //加载ViewPager
        initViewPager();
        //加载底部圆点
        initPoint();
    }

    /**
     * 加载底部圆点
     */
    private void initPoint() {
        //这里实例化LinearLayout
        vg = (ViewGroup) findViewById(R.id.guide_ll_point);
        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            iv_point = new ImageView(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
            lp.setMargins(12, 0, 12, 0);
            iv_point.setLayoutParams(lp);
            ivPointArray[i] = iv_point;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                iv_point.setBackgroundResource(R.drawable.full_holo);
                //iv_point.setBackgroundResource(R.drawable.ic_point);
            } else {
                iv_point.setBackgroundResource(R.drawable.empty_holo);
                //iv_point.setBackgroundResource(R.drawable.ic_point_active);
            }
            //将数组中的ImageView加入到ViewGroup
            vg.addView(ivPointArray[i]);
        }
    }
        //加载ViewPager
    private void initViewPager() {
        //实例化图片资源
        imageIdArray = new int[]{R.drawable.glide1, R.drawable.glide2, R.drawable.glide3};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0;i<len;i++){
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(imageIdArray[i]);

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        vp.setAdapter(new GuidePageAdapter(viewList));
        //设置滑动监听
        vp.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }
    /**
     * 滑动后的监听
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0;i<length;i++){
            ivPointArray[position].setBackgroundResource(R.drawable.full_holo);
            if (position != i){
                ivPointArray[i].setBackgroundResource(R.drawable.empty_holo);
            }
        }

        //判断是否是最后一页，若是则显示按钮
        if (position == imageIdArray.length - 1){
            guide_ib_start.setVisibility(View.VISIBLE);
        }else {
            guide_ib_start.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.guide_ib_start:  //立即体验
                //保存是否第一次进入引导页
                SpUtils.put(this,"isFirst",true);
                startActivity(new Intent(this, SplashActivity.class));
                AppManagerDelegate.getInstance().finishActivity(GlideActivity.class);
                //finish();
                break;
        }
    }
}
